package cn.jobstart.spider.listener;

import cn.jobstart.common.SpringUtils;
import cn.jobstart.enums.SpiderMonitorStatusEnum;
import cn.jobstart.modelExt.SpiderMonitorExt;
import cn.jobstart.service.ISpiderMonitorService;
import cn.jobstart.spider.spider.AbstractSpider;
import us.codecraft.webmagic.Request;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-09-19 20:47
 */
public class SkSpiderListener implements ISkSpiderListener {


    private final AtomicInteger successCount = new AtomicInteger(0);
    private final AtomicInteger errorCount = new AtomicInteger(0);
    private final ISpiderMonitorService spiderMonitorService;
    private SpiderMonitorExt spiderMonitor;
    private AbstractSpider spider = null;
    private Date startTime;
    private Date endTime;
    private int status;


    public SkSpiderListener(SpiderMonitorExt spiderMonitor) {
        this.spiderMonitor = spiderMonitor;
        this.spiderMonitorService = (ISpiderMonitorService) SpringUtils.getApplicationContext().getBean("spiderMonitorService");
    }


    public Date getStartTime() {
        return startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public AtomicInteger getSuccessCount() {
        return successCount;
    }

    public AtomicInteger getErrorCount() {
        return errorCount;
    }


    private int getStatus(String name) {
        if ("Running".equalsIgnoreCase(name)) {
            return 1;
        }
        if ("Init".equalsIgnoreCase(name)) {
            return 0;
        }
        if ("Stopped".equalsIgnoreCase(name)) {
            return 2;
        }
        return 0;


    }

    @Override
    public void onSuccess(Request request) {
        successCount.incrementAndGet();
        status = SpiderMonitorStatusEnum.RUNNING.getType();
        spiderMonitor.setSussPage(successCount.get());
        spiderMonitor.setStatus(status);
        spiderMonitor = spiderMonitorService.addLog(spiderMonitor);

    }

    @Override
    public void onError(Request request) {
        errorCount.incrementAndGet();
        status = SpiderMonitorStatusEnum.RUNNING.getType();
        spiderMonitor.setErrorPage(errorCount.get());
        spiderMonitor.setStatus(status);
        spiderMonitor = spiderMonitorService.addLog(spiderMonitor);
    }


    /**
     * 开始运行
     *
     * @param spider
     */
    @Override
    public void onStart(AbstractSpider spider) {
        this.spider = spider;
        startTime = new Date();
        spiderMonitor.setStartTime(startTime);

        status = SpiderMonitorStatusEnum.INIT.getType();
        spiderMonitor.setStatus(status);
        spiderMonitor = spiderMonitorService.addLog(spiderMonitor);


    }

    public int getStatus() {
        return status;
    }

    /**
     * 停止
     *
     * @param spider
     */
    @Override
    public void onStop(AbstractSpider spider) {
        endTime = new Date();
        status = SpiderMonitorStatusEnum.STOPPED.getType();
        spiderMonitor.setEndTime(endTime);
        spiderMonitor.setStatus(status);
        spiderMonitor = spiderMonitorService.addLog(spiderMonitor);
    }


}
